package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.common.ThreadLocalCache;
import com.sneaker.shower.entity.dto.*;
import com.sneaker.shower.service.KefuService;
import com.sneaker.shower.service.SubOrderService;
import com.sneaker.shower.service.WxMaPortalService;
import com.sneaker.shower.socket.Channel;
import com.sneaker.shower.socket.repository.ChannelRepository;
import com.sneaker.shower.socket.repository.GroupRepository;
import com.sneaker.shower.util.JacksonUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class KeFuController {
    private final KefuService keFuService;
    private final WxMaPortalService wxMaPortalService;
    private final ChannelRepository channelRepository;
    private final GroupRepository<Channel> groupRepository;
    private final SubOrderService subOrderService;

    @Autowired
    public KeFuController(KefuService keFuService, WxMaPortalService wxMaPortalService, ChannelRepository channelRepository, GroupRepository<Channel> groupRepository, SubOrderService subOrderService) {
        this.keFuService = keFuService;
        this.wxMaPortalService = wxMaPortalService;
        this.channelRepository = channelRepository;
        this.groupRepository = groupRepository;
        this.subOrderService = subOrderService;
    }

    @PostMapping("/kefu/callback")
    public String callBack(@RequestBody WxKeFuMsgDTO wxKeFuMsgDTO) {
        log.info("微信小程序推送的消息内容：{}", JacksonUtils.writeValueAsString(wxKeFuMsgDTO));
        return keFuService.handleMsg(wxKeFuMsgDTO);
    }

    @GetMapping("/kefu/callback")
    public String baseCallBack(WxCommonReqDTO wxCommonReqDTO) {
        log.info("收到客服微信回调：{}", JacksonUtils.writeValueAsString(wxCommonReqDTO));
        return wxMaPortalService.verify(wxCommonReqDTO);
    }


    @GetMapping("/kefu/history/{type}")
    public BaseView<List<SessionDTO>> history(@PathVariable("type") Integer type) {
        log.info("查询用户消息列表请求类型：{}", type);
        return new BaseView<>(keFuService.history(Long.valueOf(ThreadLocalCache.get().getGuid()), type));
    }

    @GetMapping("/kefu/list_session")
    @ApiOperation("客服消息列表")
    public BaseView<List<SessionListDTO>> listSession(@ApiParam("reqData为用户名搜索条件") SessionMsgReqDTO sessionMsgReqDTO) {
        log.info("客服消息列表请求入参：{}", JacksonUtils.writeValueAsString(sessionMsgReqDTO));
        return new BaseView<>(keFuService.listSession(sessionMsgReqDTO));
    }

    @GetMapping("/kefu/list_msg")
    @ApiOperation("用户消息内容列表")
    public BaseView<MessageRespDTO> listMsg(@ApiParam("reqData为sessionGuid") SessionMsgReqDTO sessionMsgReqDTO) {
        return new BaseView<>(keFuService.listMsg(sessionMsgReqDTO));
    }

    @GetMapping("/kefu/history/more")
    public BaseView<List<MessageDTO>> history(@ApiParam("会话guid") String sessionGuid, @ApiParam("指定记录的guid/返回这条记录以前的记录") Long minMessageGuid) {
        return new BaseView<>(keFuService.history(Long.parseLong(sessionGuid), minMessageGuid, 0));
    }

    @PostMapping("/kefu/message")
    public BaseView<String> message(@RequestBody MessageDTO message) {
        log.info("客服发送消息请求入参：{}", JacksonUtils.writeValueAsString(message));
        return new BaseView<>(keFuService.message(message));
    }

    @PostMapping("/kefu/update_order")
    @ApiOperation("修改订单信息")
    public BaseView<Boolean> updateOrder(@RequestBody SubOrderDTO subOrderDTO) {
        return new BaseView<>(subOrderService.updateOrder(subOrderDTO));
    }

    @PostMapping("/kefu/edit/kuaidi")
    @ApiOperation("修改订单快递信息")
    public BaseView<Boolean> updateOrder(@RequestBody KuaiDiNumberDTO subOrderDTO) {
        return new BaseView<>(subOrderService.updateOrderKuaidi(subOrderDTO));
    }

    @GetMapping("/kefu/receive")
    @ApiOperation("客服确认收货")
    public BaseView<Boolean> receive(String guid) {
        return new BaseView<>(subOrderService.receive(guid));
    }

    @GetMapping("/kefu/check_receive")
    @ApiOperation("客服处理完成，向用户确认订单及收货信息")
    public BaseView<Boolean> checkReceive(String guid) {
        return new BaseView<>(subOrderService.checkReceive(guid));
    }

    @GetMapping("/kefu/order_notify")
    @ApiOperation("客服处理中出现问题，可调用该接口通知用户")
    public BaseView<Boolean> orderNotify(String guid) {
        return new BaseView<>(subOrderService.orderNotify(guid));
    }

    @GetMapping("/kefu/change")
    @ApiOperation(value = "会话指派给其它客服")
    public BaseView<Boolean> change(String sessionGuid, String sysUserGuid) {
        return new BaseView<>(keFuService.change(sessionGuid, sysUserGuid));
    }

    @GetMapping("/kefu/channels")
    @ApiOperation(value = "会话指派给其它客服")
    public BaseView<Object> channels() {
        return new BaseView<>(channelRepository.peek(null));
    }

    @GetMapping("/kefu/groups")
    @ApiOperation(value = "会话指派给其它客服")
    public BaseView<Object> groups() {
        return new BaseView<>(groupRepository.peek(null));
    }

    @GetMapping("/kefu/read_message/{sessionGuid}")
    public BaseView<Boolean> readMessage(@PathVariable("sessionGuid") String sessionGuid) {
        return new BaseView<>(keFuService.readMessage(sessionGuid));
    }
}
